function promiseAll(promises) {
    return new Promise((resolve, reject)=> {
        if(!Array.isArray(promises)) {
            throw new Error('error')
        }
        let count = 0, len = promises.length, result = []
        for(let i = 0; i < len; i++) {
            promises[i].then(res => {
                count++
                result[i] = res
                if(count === len) resolve(result)
            }, err => {
                reject(err)
            })
        }
    })
}

function promiseRace(promises) {
    return new Promise((resolve, reject)=> {
        if(!Array.isArray(promises)) {
            throw new Error('error')
        }
        promises.forEach(p => p.then(res => resolve(res), err => reject(err)))
    })
}

let p1 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(1)
    }, 1000)
})
let p2 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(2)
    }, 2000)
})
let p3 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(3)
    }, 3000)
})

promiseAll([p3, p1, p2]).then(res => {
    console.log(res) // [3, 1, 2]
})

promiseRace([p3, p1, p2]).then(res => {
    console.log(res) // [3, 1, 2]
})
